// ignore_for_file: unused_import, empty_catches, library_private_types_in_public_api, deprecated_member_use, use_build_context_synchronously, avoid_print

import 'dart:io';

import 'package:customer_log_app/global/console.dart';
import 'package:customer_log_app/global/global.dart';
import 'package:customer_log_app/view/home/home.dart';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Main extends StatefulWidget {
  const Main({super.key});
  @override
  _MainState createState() => _MainState();
}

class _MainState extends State<Main> {
  int itemPerRow = 3;
  int rowIndex = -1;
  late List<Widget> posterList = [];
  late Widget bodyItems = Container();

  @override
  Widget build(BuildContext context) {
    loadData();
    return Scaffold(
      body: Scaffold(
        appBar: AppBar(
          title: Text(
            Global.trans('home', 'home_page'),
            style: TextStyle(
              fontSize: 18,
              color: Colors.white,
              fontFamily: Global.baseFont,
            ),
          ),
          centerTitle: true,
          backgroundColor: Global.appBarColor,
        ),
        body: bodyItems,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  void loadData() {
    Global.showLoading(context);
    Widget bodyItem = GridView.builder(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3, // 每行显示3个子项
      ),
      itemCount: Global.menus.length,
      itemBuilder: (BuildContext context, int index) {
        return GestureDetector(
          child: DecoratedBox(
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadiusDirectional.circular(10),
            ),
            child: Container(
              margin: const EdgeInsets.only(
                  left: 10, right: 10, top: 10, bottom: 10),
              color: Colors.brown,
              child: Center(
                child: Text(
                  Global.menus[index]["value"],
                  style: TextStyle(
                    fontFamily: Global.baseFont,
                    fontSize: 14.0,
                    // color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
          onTap: () {
            print(" click $index");
          },
        );
      },
    );

    // var allData = Global.menus;
    var width = Global.appWidth;

    setState(() {
      bodyItems = SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              width: width,
              height: Global.appHeight,
              child: bodyItem,
            ),
          ],
        ),
      );
    });

    Global.hideLoading(context);
  }
}
